package com.yanqu.road.server.cmd.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityRewardResult;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_ACTIVITY_GET_ALL_TASK_REWARD, desc = "获取活动任务奖励")
public class GetActivityTaskRewardCmd extends AbstractCommand {

    private int activityId;

    private List<Integer> conditionIdList;

    private ActivityRewardResult rewardResult;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ActivityProto.ActivityTaskGetRewardReqMsg reqMsg = ActivityProto.ActivityTaskGetRewardReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionIdList = new ArrayList<>(reqMsg.getConditionIdList());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        //todo 其他活动

        if(null == activityInfo){
            rewardResult = new ActivityRewardResult();
            rewardResult.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return;
        }
        //新服福利限量领取则需要另外判断 (客户端没给list)
        if (activityInfo.getType()==eActivityType.NewServerWelfareActivity.getValue()){
            List<Integer> conditionList = new ArrayList<>();
            BaseActivityData baseActivityData = player.getModule(ActivityModule.class).getBaseActivityData(activityId);
            List<BaseActivityCondition> activityConditionList = baseActivityData.getBaseActivityConditionList();
            for (BaseActivityCondition baseActivityCondition:activityConditionList){
                conditionList.add(baseActivityCondition.getConditionId());
            }
            conditionIdList = conditionList;
        }

        //基金
        if(activityInfo.getType() == eActivityType.WelfareActivity.getValue() && activityInfo.getChildType() == 5){
            if(player.getTitleId() < GameConfig.QUICK_RECEIVE_LIMIT_FUND){
                rewardResult = new ActivityRewardResult();
                rewardResult.setRet(GameErrorCode.E_TITLE_LEVEL_NO_ENOUGH);
                return;
            }
        }else {
            //其他活动
            if (player.getTitleId() < GameConfig.QUICK_RECEIVE_LIMIT_ACTIVITY_TASK) {
                rewardResult = new ActivityRewardResult();
                rewardResult.setRet(GameErrorCode.E_TITLE_LEVEL_NO_ENOUGH);
                return;
            }
        }

        rewardResult = player.getModule(ActivityModule.class).getActivityRewardOneKey(activityId, conditionIdList);

    }

    @Override
    public void sendData(GamePlayer player) {
        ActivityProto.ActivityTaskGetRewardRespMsg.Builder respMsg = ActivityProto.ActivityTaskGetRewardRespMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.addAllConditionId(conditionIdList);
        respMsg.setRet(rewardResult.getRet());
        if(rewardResult.getRet() == 0){
            respMsg.setReward(rewardResult.getReward());
        }
        player.sendPacket(Protocol.U_ACTIVITY_GET_ALL_TASK_REWARD, respMsg);

    }
}
